
"""
元组的相关操作(只读, 无法修改) --> 不可变对象. --> 可哈希
"""

# 创建元组
a = (1, 2, 3, 4, 'hello', True, [1, 'world', False], (1, 'world', False))
print(type(a), a, len(a))
b = tuple()
print(type(b), b, len(b))
print("-------------------------------------")


# 通过下标访问元组的元素
print(a[4], a[6])
print(a[-1])
print("-------------------------------------")


# 元组的切片操作
print(a[:])
print(a[1:5])
print(a[1:len(a)])
print(a[-3:100])
print("-------------------------------------")


# 元组的遍历
a = (1, 2, 3, 4, 'hello', True, [1, 'world', False], (1, 'world', False))
for elem in a:
    print(elem, end=" ")
print()

i = 0
while i < len(a):
    print(a[i], end=' ')
    i += 1
print()
print("-------------------------------------")


# 元组的查找, in 和 index 方法
a = (1, 2, 3, 4, 'hello', True, [1, 'world', False], (1, 'world', False))
print(1 in a)
print(1 not in a)
print(100 in a)
print(100 not in a)

print(a.index(4))  # 3
# print(a.index(60))  # 抛异常

# 元组的拼接
a = (1, 2, 3, 4)
b = (5, 6, 7, 8)
print(a + b)
print(b + a)
print("-------------------------------------")


# 当进行多元赋值的时候, 其实本质上是按照元组的方式进行工作的
def getPoint():
    x = 10
    y = 20
    return x, y


c, d = getPoint()
print(type(getPoint()))  # 返回的类型是一个元组
print(c, d)

